package com.abc.recycling.controller.admin;

import com.abc.recycling.model.StatisticsData;
import com.abc.recycling.service.StatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final StatisticsService statisticsService;
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 统计报表页面
     */
    @GetMapping
    public String statistics(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime,
            Model model
    ) {
        // 如果未指定时间范围，默认显示最近30天的数据
        if (startTime == null) {
            startTime = LocalDateTime.now().minusDays(30).withHour(0).withMinute(0).withSecond(0);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        }

        // 获取统计数据
        StatisticsData data = statisticsService.getStatisticsData(startTime, endTime);
        model.addAttribute("data", data);
        model.addAttribute("startTime", startTime.format(DATE_FORMATTER));
        model.addAttribute("endTime", endTime.format(DATE_FORMATTER));

        return "admin/statistics";
    }

    /**
     * 获取订单趋势数据
     */
    @GetMapping("/order-trend")
    @ResponseBody
    public List<Map<String, Object>> getOrderTrend(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime,
            @RequestParam(defaultValue = "7") int points
    ) {
        return statisticsService.getOrderTrend(startTime, endTime, points);
    }

    /**
     * 获取物品分布数据
     */
    @GetMapping("/item-distribution")
    @ResponseBody
    public List<Map<String, Object>> getItemDistribution(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime
    ) {
        return statisticsService.getItemDistribution(startTime, endTime);
    }

    /**
     * 获取回收员排名
     */
    @GetMapping("/top-collectors")
    @ResponseBody
    public List<Map<String, Object>> getTopCollectors(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime,
            @RequestParam(defaultValue = "10") int limit
    ) {
        return statisticsService.getTopCollectors(startTime, endTime, limit);
    }

    /**
     * 获取回收站排名
     */
    @GetMapping("/top-stations")
    @ResponseBody
    public List<Map<String, Object>> getTopStations(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime,
            @RequestParam(defaultValue = "10") int limit
    ) {
        return statisticsService.getTopStations(startTime, endTime, limit);
    }

    /**
     * 获取区域排名
     */
    @GetMapping("/top-areas")
    @ResponseBody
    public List<Map<String, Object>> getTopAreas(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime,
            @RequestParam(defaultValue = "10") int limit
    ) {
        return statisticsService.getTopAreas(startTime, endTime, limit);
    }

    /**
     * 导出统计报表
     */
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportReport(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime endTime
    ) {
        byte[] report = statisticsService.exportStatisticsReport(startTime, endTime);
        
        String filename = String.format("统计报表_%s_%s.xlsx",
                startTime.format(DATE_FORMATTER),
                endTime.format(DATE_FORMATTER));
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", filename);
        
        return ResponseEntity.ok()
                .headers(headers)
                .body(report);
    }
} 